package com.example.hp.yuejian5.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.example.hp.yuejian5.R;
import com.example.hp.yuejian5.other.GlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import static com.example.hp.yuejian5.other.url.BURL1;
import static com.example.hp.yuejian5.other.url.BURL2;
import static com.example.hp.yuejian5.other.url.BURL3;
import static com.example.hp.yuejian5.other.url.BURL4;

/**
 * A simple {@link Fragment} subclass.
 */
public class IndexFragment extends Fragment {


    private Banner banner;
    private List<String> mListImage;
    private RadioButton activity_radio,match_radio,train_radio;
    private ActivityFragment activityFragment;
    private CompetitionFragment competitionFragment;
    private TrainFragment trainFragment;
    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;
    public IndexFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }
    private void initView(View view)
    {
        banner=view.findViewById(R.id.index_banner);
        activity_radio=view.findViewById(R.id.activity_radio);
        match_radio=view.findViewById(R.id.match_radio);
        train_radio=view.findViewById(R.id.train_radio);
        banner.setBannerStyle(BannerConfig.NUM_INDICATOR);
        banner.setImageLoader(new GlideImageLoader());
        mListImage = new ArrayList<>();
        mListImage.add(BURL1);
        mListImage.add(BURL2);
        mListImage.add(BURL3);
        mListImage.add(BURL4);

        banner.setImages(mListImage);
        banner.setBannerAnimation(Transformer.DepthPage);
        banner.setDelayTime(2000);
        banner.setIndicatorGravity(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        banner.start();
        initEvents();
    }

    private void initEvents()
    {
            activity_radio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked)
                    {
                        if (activityFragment==null)
                        {
                            activityFragment=new ActivityFragment();
                        }
                        fragmentTransaction=fragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.index_fragmentlayout,activityFragment);
                        fragmentTransaction.commit();
                    }
                }
            });

            match_radio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked)
                    {
                        if (competitionFragment==null)
                        {
                           competitionFragment=new CompetitionFragment();
                        }
                        fragmentTransaction=fragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.index_fragmentlayout,competitionFragment);
                        fragmentTransaction.commit();
                    }
                }
            });
            train_radio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked)
                    {
                        if (trainFragment==null)
                        {
                           trainFragment=new TrainFragment();
                        }
                        fragmentTransaction=fragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.index_fragmentlayout,trainFragment);
                        fragmentTransaction.commit();
                    }
                }
            });

        fragmentManager=getActivity().getSupportFragmentManager();
        activity_radio.setChecked(true);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_index, container, false);
    }

}
